/**
 * Copyright (c) 2006 eMundo GmbH. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * Project: agil-client-web
 * File: GwtToClientEjbProxy.java
 *
 */

package eu.emundo.agilpro.web.server;

import org.apache.log4j.Logger;

import eu.emundo.agilpro.ifw.service.ejb.remote.Client;

/**
 * The communication between the Gwt-Client and Gwt-Server is asynchron. To
 * avoid concurrent ejb-calls, this asynchonous communication must be translated
 * in a synchronous one.
 * 
 * @author Fabian Wohlschl\u00E4ger
 */
public class GwtToClientEjbProxy {
	/**
	 * Logger
	 */
	private final static Logger sLogger = Logger.getLogger(GwtToClientEjbProxy.class.getName());

	/**
	 * ClientEjb
	 */
	private Client mClient;

	/**
	 * True if there is already a invoke of the ClientEJB Method
	 */
	private boolean mClientEjbInUseFlag = false;
	
	/**
	 * Time to wait if the ClientEjb is already invoked
	 */
	private static int WAIT_INTERVALL = 100;
	
	/**
	 * Constructor
	 * 
	 * @param pClient
	 */
	public GwtToClientEjbProxy(Client pClient) {
		mClient = pClient;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#cancelAction()
	 */
	public void cancelAction() {
		sLogger.debug("cancelAction");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.cancelAction();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#changedRole(java.lang.Object)
	 */
	public void changedRole(Object pRole) {
		sLogger.debug("changedRole");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.changedRole(pRole);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#delayAction()
	 */
	public void delayAction() {
		sLogger.debug("delayAction");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.delayAction();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#getActivitiesForRole(java.lang.Object)
	 */
	public Object[] getActivitiesForRole(Object pRole) {
		sLogger.debug("getActivitiesForRole");
		
		Object[] lActivities;
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				lActivities = mClient.getActivitiesForRole(pRole);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
		
		return lActivities;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#getDelayedActivitiesForRole(java.lang.Object)
	 */
	public Object[] getDelayedActivitiesForRole(Object pRole) {
		sLogger.debug("getDelayedActivitiesForRole");
		
		Object[] lDelayedActivities;
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				lDelayedActivities = mClient.getDelayedActivitiesForRole(pRole);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
		
		return lDelayedActivities;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#getProcessContext()
	 */
	public byte[] getProcessContext() {
		sLogger.debug("getProcessContext");
		
		byte[] lPcSerialized;
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				lPcSerialized = mClient.getProcessContext();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
		
		return lPcSerialized;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#getRoles()
	 */
	public Object[] getRoles() {
		sLogger.debug("getRoles");
		
		Object[] lRoles;
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				lRoles = mClient.getRoles();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
		
		return lRoles;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#getTicketNumber()
	 */
	public Number getTicketNumber() {
		sLogger.debug("getTicketNumber");
		
		Number lTicketNumber;
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				lTicketNumber = mClient.getTicketNumber();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
		
		return lTicketNumber;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#newTicketNumber()
	 */
	public void newTicketNumber() {
		sLogger.debug("newTicketNumber");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.newTicketNumber();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#nextAction()
	 */
	public void nextAction() {
		sLogger.debug("nextAction");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.nextAction();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#resumeDelayedActivity(java.lang.Object)
	 */
	public void resumeDelayedActivity(Object pActivity) {
		sLogger.debug("resumeDelayedActivity");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.resumeDelayedActivity(pActivity);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#setProcessContext(byte[])
	 */
	public void setProcessContext(byte[] pPcSerialized) {
		sLogger.debug("setProcessContext");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.setProcessContext(pPcSerialized);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#setTicketNumber(java.lang.Number)
	 */
	public void setTicketNumber(Number pTicketNumber) {
		sLogger.debug("setTicketNumber");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.setTicketNumber(pTicketNumber);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#startActivity(java.lang.Object)
	 */
	public void startActivity(Object pActivity) {
		sLogger.debug("startActivity");
		
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.startActivity(pActivity);
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.emundo.agilpro.ifw.service.ejb.remote.Client#stopSession()
	 */
	public void stopSession() {
		// To handle concurrent calls on the StatefulSessionBean
		while (true) {
			if (!mClientEjbInUseFlag) {
				mClientEjbInUseFlag = true;
				mClient.stopSession();
				mClientEjbInUseFlag = false;
				break;
			}

			try {
				Thread.sleep(WAIT_INTERVALL);
			} catch (InterruptedException e1) {
				sLogger.warn("Thread sleeping for next call failed - " + e1);
			}
		}
	}
}
